﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;

namespace ENG.TeachersDiary
{
  public partial class FrmIntro : Form
  {
    public FrmIntro()
    {
      InitializeComponent();
    }

    private ENG.TeachersDiary.Model.DiaryDB db = null;

    private void frmIntro_Load(object sender, EventArgs e)
    {
      db = Common.DBConn;

      schoolYearBindingSource.DataSource = db.SchoolYears;
      subjectBindingSource.DataSource = db.Subjects;
      cmbSchoolYears_SelectedIndexChanged(this, null);
      cmbClasses_SelectedIndexChanged(this, null);

      try
      {
        cmbSchoolYears.SelectedIndex = Properties.Settings.Default.LastSchoolYearIndex;
        cmbClasses.SelectedIndex = Properties.Settings.Default.LastClassIndex;
        cmbSubject.SelectedIndex = Properties.Settings.Default.LastSubjectIndex;
      }
      catch (Exception)
      { }
    }

    private void btnCreateNewSchoolYear_Click(object sender, EventArgs e)
    {
      FrmCreateSchoolYear f = new FrmCreateSchoolYear();

      f.ShowDialog();

      if (f.DialogResult != System.Windows.Forms.DialogResult.OK)
        return;

      // this is exceptional behavior, will generate and also save new schoolyear !
      ENG.TeachersDiary.Model.SchoolYear sy = f.GenerateSchoolYear(db);

      schoolYearBindingSource.Add(sy);
      db.SubmitChanges();
    }

    public ENG.TeachersDiary.Model.Class SelectedClass
    {
      get
      {
        Object pom = cmbClasses.SelectedItem;

        ENG.TeachersDiary.Model.Class ret = (ENG.TeachersDiary.Model.Class)pom;

        return ret;
      }
    }
    public ENG.TeachersDiary.Model.Subject SelectedSubject
    {
      get
      {
        object pom = cmbSubject.SelectedItem;

        ENG.TeachersDiary.Model.Subject ret = pom as ENG.TeachersDiary.Model.Subject;

        return ret;
      }
    }

    private void cmbSchoolYears_SelectedIndexChanged(object sender, EventArgs e)
    {
      btnCreateClass.Enabled = (cmbSchoolYears.SelectedItem != null);
    }

    private void cmbClasses_SelectedIndexChanged(object sender, EventArgs e)
    {
      btnOpen.Enabled =
        (cmbClasses.SelectedItem != null)
        &&
        (cmbSubject.SelectedItem != null);
    }

    private void btnCreateClass_Click(object sender, EventArgs e)
    {
      FrmCreateClass f = new FrmCreateClass();

      f.ShowDialog();

      if (f.DialogResult != System.Windows.Forms.DialogResult.OK)
        return;

      string newTitle = f.txtClassTitle.Text.Trim();

      ENG.TeachersDiary.Model.Class c = new ENG.TeachersDiary.Model.Class();
      c.Title = newTitle;
      c.SchoolYearID = (cmbSchoolYears.SelectedItem as Model.SchoolYear).SchoolYearID;

      classesBindingSource.Add(c);

      db.SubmitChanges();
    }

    private void btnCreateSubject_Click(object sender, EventArgs e)
    {
      FrmCreateSubject f = new FrmCreateSubject();

      f.ShowDialog();

      if (f.DialogResult != System.Windows.Forms.DialogResult.OK)
        return;

      string newTitle = f.txtSubjectTitle.Text.Trim();
      string newAbbr = f.txtSubjectAbbr.Text.Trim();

      ENG.TeachersDiary.Model.Subject s = new ENG.TeachersDiary.Model.Subject();
      s.Title = newTitle;
      s.Abbr = newAbbr;

      subjectBindingSource.Add(s);

      var mg = Common.CreateDefaultMarkGroupWithoutSubject();

      mg.Subject = s;
      db.MarkGroups.InsertOnSubmit(mg);
      db.SubmitChanges();
    }

    private void btnOptions_Click(object sender, EventArgs e)
    {
      var db = Properties.Settings.Default.DBFile;

      FrmOptions f = new FrmOptions();

      f.ShowDialog();

      if (db != Properties.Settings.Default.DBFile)
        Application.Restart();
    }

    private void btnOpen_Click(object sender, EventArgs e)
    {
      Properties.Settings.Default.LastSchoolYearIndex = cmbSchoolYears.SelectedIndex;
      Properties.Settings.Default.LastClassIndex = cmbClasses.SelectedIndex;
      Properties.Settings.Default.LastSubjectIndex = cmbSubject.SelectedIndex;
      Properties.Settings.Default.Save();
    }

  }
}
